﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using TSB.FarmPowerShellWizard.Entities;

namespace TSB.FarmPowerShellWizard.Repository
{
    public class WebApplicationRepository
    {
        
   

        public List<WebApplication> GetWebApplications()
        {
            var webApps = new List<WebApplication>();
            foreach (var wa in SPWebService.ContentService.WebApplications)
            {
                webApps.Add(new WebApplication
                                {
                                    Name = wa.Name,
                                    Id = wa.Id
                                });
            }

            return webApps;
        }

        private string psCmd = string.Empty;
        private StringBuilder ps;
        public string GetWebApplicationScript(Guid webAppId)
        {
            ps = new StringBuilder();

            var wa = SPWebService.ContentService.WebApplications.Where(x => x.Id == webAppId).First();
            var iisDefault = wa.IisSettings[SPUrlZone.Default];
            var iisUrl = string.IsNullOrEmpty(iisDefault.ServerBindings[0].HostHeader) ? Environment.MachineName : iisDefault.ServerBindings[0].HostHeader ;
            var iisPort = iisDefault.ServerBindings[0].Port;
            
            ps.AppendLine("########################################\n# Web Application \n########################################");
            ps.AppendFormat("\n$webAppName = \"{0}\"", wa.Name);
            ps.AppendFormat("\n$webAppPort = {0}", iisPort);
            ps.AppendFormat("\n$webAppUrl = \"{0}\"", iisUrl);
            ps.AppendFormat("\n$webAppHostHeader = \"{0}\"", iisUrl);
            ps.AppendFormat("\n$webAppAppPool = \"{0}\"", wa.ApplicationPool.Name);
            ps.AppendFormat("\n$webAppAppPoolUsername = \"{0}\"", wa.ApplicationPool.ManagedAccount.DisplayName);
            ps.AppendFormat("\n$databaseServer = \"{0}\"", wa.ContentDatabases[0].Server);
            ps.AppendFormat("\n$databaseName = \"{0}\"", wa.ContentDatabases[0].Name);

            psCmd = Helpers.GetScriptComment("WEBAPP_NewSPWebApp");

            GetZone(iisDefault);
           
            ps.AppendLine(psCmd);
            
            var rootSite = wa.Sites[0];

            ps.AppendFormat(Helpers.GetScriptComment("WEBAPP_NewSPSite"), rootSite.HostName, rootSite.Owner.LoginName, rootSite.RootWeb.WebTemplate, rootSite.RootWeb.Configuration);

            return ps.ToString();
        }

        void GetZone(SPIisSettings iisSettings)
        {
            if (iisSettings.AllowAnonymous)
            {
                psCmd += " -AllowAnonymous";
            }

            if (iisSettings.UseBasicAuthentication || !iisSettings.UseWindowsIntegratedAuthentication || !iisSettings.DisableKerberos)
            {
                var authentProviders = "\n$ap = New-SPAuthenticationProvider";

                if (iisSettings.UseWindowsIntegratedAuthentication)
                {
                    if (iisSettings.DisableKerberos)
                    {
                        authentProviders += " -UseWindowsIntegratedAuthentication";
                    }
                    else
                    {
                        authentProviders += " -DisableKerberos:$false";
                    }
                }

                if (iisSettings.UseBasicAuthentication)
                {
                    authentProviders += " -UseBasicAuthentication";
                }

                ps.AppendLine(authentProviders);
                psCmd += " -AuthenticationProvider $ap";
            }

            if (iisSettings.UseClaimsAuthentication)
            {
                ps.AppendLine(Helpers.GetComment("WEBAPP_AuthClaims"));
            }
            if (iisSettings.UseFormsClaimsAuthenticationProvider)
            {
                ps.AppendLine(Helpers.GetComment("WEBAPP_AuthForms"));
            }
            if (iisSettings.UseTrustedClaimsAuthenticationProvider)
            {
                ps.AppendLine(Helpers.GetComment("WEBAPP_AuthTrusted"));
            }
        }
    }
}

